package com.geostar.constructionland.analyse.service.handler.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.service.analyseresult.IStbhhxService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author makoto_zhu
 * @Date 2021-03-16 10:16:46
 * @Description
 **/
@Component
@Slf4j
public class MineGjdzgyHandler implements IResultHandler {
    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    IStbhhxService stbhhxService;

    public static Map<String,String> resultMap = new HashMap<>() ;

    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        log.info("处理生态保护红线分析服务,状态码:{}", httpResult.getCode());
        HandlerResult result = HandlerResult.createBySuccess();
        if (HttpStatus.SC_OK == httpResult.getCode()) {
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(), AlyResult.class);
            log.info("body:" + httpResult.getBody());
            if (HttpStatus.SC_OK == alyResult.getStatus()) {
                result.setData(httpResult.getBody());

                if(resultMap.containsKey(analyseModel.getAlyId())) {
                    String lastResult = resultMap.get(analyseModel.getAlyId());
                    String combinedResult = combineResult(lastResult,httpResult.getBody());
                    result.setData(combinedResult);
                    resultMap.remove(analyseModel.getAlyId());
                } else {
                    resultMap.put(analyseModel.getAlyId(),httpResult.getBody());
                }

            } else {
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;

    }

    private String combineResult(String first,String second) {
        String result ;
        try {
            Map<String,Object> firstMap = objectMapper.readValue(first,Map.class);
            Map<String,Object> secondMap = objectMapper.readValue(second,Map.class);

            ArrayList firstList = (ArrayList)firstMap.get("Results");
            ArrayList secondList = (ArrayList)secondMap.get("Results");

            CollectionUtils.addAll(firstList,secondList.toArray());

            firstMap.put("Results",firstList);
            result = objectMapper.writeValueAsString(firstMap);
        } catch (IOException e) {
            e.printStackTrace();
            result = first;
        }
        return result;
    }
}
